define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            //绑定select元素事件
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "任务名称-搜索";};
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'task_config2/index' + location.search,
                    add_url: 'task_config2/add',
                    edit_url: 'task_config2/edit',
                    del_url: 'task_config2/del',
                    multi_url: 'task_config2/multi',
                    import_url: 'task_config2/import',
                    table: 'task_config',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'Id',
                sortName: 'Id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'Id', title: __('ID')},
                        {field: 'Name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'paramstype.name', title: __('请求类型type'), operate: 'LIKE'},
                        {field: 'paramstype.name', title: __('请求类型type'), visible: true, addClass: "selectpage", extend: "data-source='params_type' data-field='name' data-primary-key='name'"},
                        {field: 'Url', title: __('Url'), operate: false, formatter: Table.api.formatter.url},
                        {field: 'Eigenvalue', title: __('Eigenvalue'), operate: false},
                        {field: 'Count', title: __('Count'), operate: false},
                        {field: 'count_actual', title: __('返回数量'),operate: false},
                        {field: 'actual_quantity', title: __('实际执行数量'),operate: false},
                        {field: 'click_nums', title: __('广告点击数量'),operate: false},
                        // {field: 'TimeStart', title: __('Timestart'),operate: false},
                        // {field: 'TimeEnd', title: __('Timeend'),operate: false},
                        // {field: 'DateStart', title: __('Datestart'),operate: false},
                        // {field: 'DateEnd', title: __('DateEnd'),operate: false},
                        // {field: 'TimeSpan', title: __('TimeSpan'), operate: true},
                        // {field: 'TimeSpan', title: __('时间间隔'),operate: false},
                        // {field: 'Detail', title: __('Detail'),operate: false,align: 'left',cellStyle: Controller.api.formatter.css},
                        // {field: 'Status', title: __('Status'),operate: false},
                        //formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
                        {field: 'create_time',
                            title: '返回数量（日期）',
                            operate:'=',
                            addclass:'datetimepicker',
                            visible: false,
                            cellStyle: Controller.api.formatter.css,
                            autocomplete:false,
                            defaultValue: (new Date()).toLocaleDateString().split('/').join('-')
                        },
                        // {field: 'create_time',
                        //     title: __('实际执行数量（日期）'),
                        //     formatter: Table.api.formatter.datetime,
                        //     operate:'RANGE',
                        //     addclass:'datetimerange',
                        //     autocomplete:false,
                        //     visible: false,
                        //     datetimeFormat:"YYYY-MM-DD"
                        // },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table',function(e,data){
                // console.log(e);
                var date_1 = $("#create_time").val();
                // console.log($("#create_time").val());
                $.ajax({
                    type: "GET",
                    url: 'task_config2/actual_quantity',
                    data: {'date':date_1},
                    dataType: "json", //声明成功使用json数据类型回调
                    success: function(res) {

                        for (var i = 0; i < res.length; i++) {
                            let round_config_id = res[i].round_config_id;
                            let total = res[i].total;
                            $('#table tbody tr').each(function(){
                                let id = $(this).find('td').eq(1).html();
                                if (round_config_id == id) {
                                    $(this).find('td').eq(8).html(total);
                                };
                            })
                        };
                    }
                })
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                css:function(){
                    return{
                        css:{
                            "max-width":"450px !important",
                            "overflow":"hidden",
                            "white-space":"nowrap",
                            "text-overflow":"ellipsis",
                            "display":"block",
                        }
                    };
                }
            }
        }
    };
    return Controller;
});
